package com.example.tass.controller;

import cn.tass.SRJ1933.devices.inf.TassSignServerApi;
import cn.tass.SRJ1933.devices.params.response.keys.GenAsymKeyPairResponse;
import cn.tass.SRJ1933.devices.params.response.keys.GenSymKeyResponse;
import cn.tass.exceptions.TAException;
import com.example.tass.common.ResultMessage;
import com.example.tass.domain.AsyKeyPair;
import com.example.tass.domain.MyGenAsymKeyPairRequest;
import com.example.tass.domain.MyGenSymKeyRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

// 3.9 对称密钥和非对称密钥
@RestController
public class GenSyncKeyAndAsyKeyTest_3_9 {
    private static String printFormat = "错误码[%d]\n错误消息[%s]\n接口结果： %s\n";
    private static final Logger logger = LoggerFactory.getLogger(GenSyncKeyAndAsyKeyTest_3_9.class);
    private TassSignServerApi signServerApi;

    public GenSyncKeyAndAsyKeyTest_3_9(TassSignServerApi signServerApi) {
        this.signServerApi = signServerApi;
    }

    // 3.9.1 生成对称密钥

    // 3.9.1.1生成对称密钥（生成对称密钥 – 明文返回）
    @GetMapping(value = "/genSymKey")
    public ResultMessage genSymKey(@RequestBody MyGenSymKeyRequest request) {
        try {
            GenSymKeyResponse response = signServerApi.genSymKey(request.getAlgName(), request.getKeySize());
            if (response.getCode() != 0) {
                logger.error(response.getMsg());
                return ResultMessage.newFailureResult(response.getMsg());
            } else {
                System.out.println("生成对称密钥（生成对称密钥 – 明文返回）： " + String.format(printFormat, response.getCode(), response.getMsg(), response.getKey()));
                return ResultMessage.newSuccessResult(response.getKey());
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }

    // 3.9.1.2生成对称密钥（生成对称密钥 – 密文返回）
    @GetMapping(value = "/genSymKeyReturnEncKey")
    public ResultMessage genSymKeyReturnEncKey(@RequestBody MyGenSymKeyRequest request) {
        try {
            GenSymKeyResponse response = signServerApi.genSymKey(request.getAlgName(), request.getKeySize(), request.getEncKeyId(),
                    request.getEnckeyAccessCode(), request.getEncMode(), request.getEncPadMode(), request.getEncIv());
            if (response.getCode() != 0) {
                logger.error(response.getMsg());
                return ResultMessage.newFailureResult(response.getMsg());
            } else {
                System.out.println("生成对称密钥（生成对称密钥 – 密文返回）： " + String.format(printFormat, response.getCode(), response.getMsg(), response.getKey()));
                return ResultMessage.newSuccessResult(response.getKey());
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }

    // 3.9.2生成非对称密钥 【接口生成非对称密钥，可生成明文密钥对和密文密钥对，密文密钥对只对私钥部分加密。】

    // 3.9.2.1生成非对称密钥（私钥明文返回）
    @GetMapping(value = "/genAsymKeyPair")
    public ResultMessage genAsymKeyPair(@RequestBody MyGenAsymKeyPairRequest request) {
        try {
            GenAsymKeyPairResponse response = signServerApi.genAsymKeyPair(request.getKeyType(), request.getRsaModulusLen(), request.getEccCurveId());
            if (response.getCode() != 0) {
                logger.error(response.getMsg());
                return ResultMessage.newFailureResult(response.getMsg());
            } else {
                System.out.println("生成非对称密钥（明文返回）： " + String.format(printFormat, response.getCode(), response.getMsg(), response.getPrivateKey() + ", " + response.getPublicKey()));
                AsyKeyPair asyKeyPair = new AsyKeyPair(response.getPrivateKey(), response.getPublicKey());
                ResultMessage resultMessage = ResultMessage.newSuccessResult();
                resultMessage.setData(asyKeyPair);
                return resultMessage;
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }

    // 3.9.2.2 生成非对称密钥（私钥密文返回）
    @GetMapping(value = "/genAsymKeyPairEncPriKey")
    public ResultMessage genAsymKeyPairEncPriKey(@RequestBody MyGenAsymKeyPairRequest request) {
        try {
            GenAsymKeyPairResponse response = signServerApi.genAsymKeyPair(request.getKeyType(), request.getRsaModulusLen(), request.getEccCurveId(),
                    request.getEncKeyId(), request.getEnckeyAccessCode(), request.getEncMode(), request.getEncPadMode(), request.getEncIv());
            if (response.getCode() != 0) {
                logger.error(response.getMsg());
                return ResultMessage.newFailureResult(response.getMsg());
            } else {
                System.out.println("生成非对称密钥（密文返回）： " + String.format(printFormat, response.getCode(), response.getMsg(), response.getPrivateKey() + ", " + response.getPublicKey()));
                AsyKeyPair asyKeyPair = new AsyKeyPair(response.getPrivateKey(), response.getPublicKey());
                ResultMessage resultMessage = ResultMessage.newSuccessResult();
                resultMessage.setData(asyKeyPair);
                return resultMessage;
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }
}
